using System;
using System.Data;
using System.Configuration;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using TanashCMS.Business;
using NHibernate;

public partial class manager_pages_CreateContent : CMSManagementPageBase
{
    private SmartPageComponent mComponent = null;
    public SmartPageComponent SmartComponent { get { return mComponent; } set { mComponent = value; } }
    private int mSlotIndex=-1;

    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected override void OnPreInit(EventArgs e)
    {
        if (Request.QueryString["pid"] != null)
            mCurrentPage = Configuration.GetCurrentSession().Get<WebPage>(int.Parse(Request.QueryString["pid"]));
        if (Request.QueryString["cid"] != null)
            mComponent = Configuration.GetCurrentSession().Get<SmartPageComponent>(int.Parse(Request.QueryString["cid"]));
        if (Request.QueryString["si"] != null)
            mSlotIndex = int.Parse(Request.QueryString["si"]);


        base.OnPreInit(e);
    }

    protected override void OnInit(EventArgs e)
    {
        if (mComponent == null)
        {
            panelChooseContentType.Visible = true;
            phContentEditor.Visible = false;
        }
        else
        {
            panelChooseContentType.Visible = false;
            phContentEditor.Visible = true;
        }

        base.OnInit(e);
    }

    protected void phContentEditor_Init(object sender, EventArgs e)
    {
        if (mComponent == null)
            return;

        SmartPageComponentControlBase smartControl = null;
        string controlPath = "";

        if (mComponent.ComponentType == ComponentType.HtmlContent)
        {
            controlPath = Request.ApplicationPath.TrimEnd('/') + "/smartpage/Components/HtmlContent.ascx";
        }
        else if (mComponent.ComponentType == ComponentType.ProductList)
        {
            controlPath = Request.ApplicationPath.TrimEnd('/') + "/smartpage/Components/ProductList.ascx";
        }

        smartControl = (SmartPageComponentControlBase)LoadControl(controlPath);
        smartControl.SmartComponent = mComponent;
        smartControl.onSaveComponent += new EventHandler(smartControl_onSaveComponent);
        smartControl.onCancelComponent += new EventHandler(smartControl_onCancelComponent);
        ((PlaceHolder)sender).Controls.Add(smartControl);
        smartControl.ChangeMode(FormViewMode.Edit);
    }

    void smartControl_onCancelComponent(object sender, EventArgs e)
    {

        Response.Redirect("build.aspx?id=" + mCurrentPage.ID.ToString());
    }

    void smartControl_onSaveComponent(object sender, EventArgs e)
    {
        Response.Redirect("build.aspx?id=" + mCurrentPage.ID.ToString());
    }
    protected void radioContentType_SelectedIndexChanged(object sender, EventArgs e)
    {

        ISession session = Configuration.GetCurrentSession();

        if (mComponent == null)
        {
            List<SmartPageComponent> components = (List<SmartPageComponent>)session.CreateQuery("select sc from SmartPageComponent sc where sc.WebPage.ID = " + mCurrentPage.ID.ToString()).List<SmartPageComponent>();
            int nextSlot = components.Count;
            mComponent = new SmartPageComponent();
            mComponent.SlotIndex = nextSlot;
            mComponent.ComponentType = (ComponentType)Enum.Parse(typeof(ComponentType), radioContentType.SelectedValue);
            mComponent.WebsiteID = mWebsite.ID;
            mComponent.WebPageID = mCurrentPage.ID;

            ITransaction tx = session.BeginTransaction();
            try
            {
                session.Save(mComponent);
                session.Flush();
                tx.Commit();
                session.Disconnect();
                Response.Redirect("CreateContent.aspx?pid=" + mCurrentPage.ID.ToString() + "&cid=" + mComponent.ID.ToString() + "&si=" + mComponent.SlotIndex.ToString());
            }
            catch (Exception ex)
            {
                tx.Rollback();
                session.Disconnect();
                Response.Write(ex.Message);
                while (ex.InnerException != null)
                {
                    ex = ex.InnerException;
                    Response.Write("<Br/>" + ex.Message);
                }
                ShowSystemFeedback("Could not create content: " + ex.Message);
            }
        }
    }
}
